﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using com.hyrentals.data;
using com.hyrentals.common;
using System.Data;
using System.Data.SqlClient;
using ServiceStack.Text;
using System.Web.Script.Serialization;


namespace com.hyrentals.dbaccess
{
    public class RVDBAccess : DBAccessBase
    {
        public RVDBAccess()
            : base(ConfigurationManager.ConnectionStrings["APPContext"].ConnectionString) { }

        public RVDBAccess(string connectionString)
            : base(connectionString) { }

        public bool InsertRV(string id, string ownerId, RVTypeEnum type, string modelStyle, 
             DateTime madeYear, RVMadeByEnum madeBy, string made, byte sleeps,
             double length, string zip, int cityId, int stateId,
             RVAvailabilityTypeEnum availableType, bool IsAllowedHandleBooking,
            RVStatus status, DateTime createTime)
         {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_OWNER_ID, DBParamNameConstant.RV_OWNER_ID_DB_TYPE, DBParamNameConstant.RV_OWNER_ID_SIZE, ownerId),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_TYPE, DBParamNameConstant.RV_TYPE_DB_TYPE, DBParamNameConstant.RV_TYPE_SIZE, (byte)type),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MODEL_STYLE, DBParamNameConstant.RV_MODEL_STYLE_DB_TYPE, DBParamNameConstant.RV_MODEL_STYLE_SIZE, modelStyle),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MADE_YEAR, DBParamNameConstant.RV_MADE_YEAR_DB_TYPE, DBParamNameConstant.RV_MADE_YEAR_SIZE, madeYear),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MADE_BY, DBParamNameConstant.RV_MADE_BY_DB_TYPE, DBParamNameConstant.RV_MADE_BY_SIZE, (byte)madeBy),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MADE, DBParamNameConstant.RV_MADE_DB_TYPE, DBParamNameConstant.RV_MADE_SIZE, made),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_SLEEPS, DBParamNameConstant.RV_SLEEPS_DB_TYPE, DBParamNameConstant.RV_SLEEPS_SIZE, sleeps),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_LENGTH, DBParamNameConstant.RV_LENGTH_DB_TYPE, DBParamNameConstant.RV_LENGTH_SIZE, length),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ZIP, DBParamNameConstant.RV_ZIP_DB_TYPE, DBParamNameConstant.RV_ZIP_SIZE, zip),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_STATE_ID, DBParamNameConstant.RV_STATE_ID_DB_TYPE, DBParamNameConstant.RV_STATE_ID_SIZE, stateId),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_CITY_ID, DBParamNameConstant.RV_CITY_ID_DB_TYPE, DBParamNameConstant.RV_CITY_ID_SIZE, cityId),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_AVAILABLE_TYPE, DBParamNameConstant.RV_AVAILABLE_TYPE_DB_TYPE, DBParamNameConstant.RV_AVAILABLE_TYPE_SIZE, (byte)availableType),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING, DBParamNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING_DB_TYPE, DBParamNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING_SIZE, IsAllowedHandleBooking),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_STATUS, DBParamNameConstant.RV_STATUS_DB_TYPE, DBParamNameConstant.RV_STATUS_SIZE, (byte)status),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_CREATE_TIME, DBParamNameConstant.RV_CREATE_TIME_DB_TYPE, DBParamNameConstant.RV_CREATE_TIME_SIZE, createTime)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_insert]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "InsertRV",
                    UserId = ownerId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
         }

        public IEnumerable<RV> GetRVByOwnerId(string ownerId)
         {
             List<RV> notices = new List<RV>();
             try
             {
                 SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_OWNER_ID, DBParamNameConstant.RV_OWNER_ID_DB_TYPE, DBParamNameConstant.RV_OWNER_ID_SIZE, ownerId)
                                        };

                 DataSet ds = DbHelper.ExecuteDataset(this._connectionString, CommandType.StoredProcedure, "[rv_get_by_ownerid]", parms);
                 
                 if (ds != null && ds.Tables.Count > 0)
                 {
                     DataTable RVTable = ds.Tables[0];
                     JavaScriptSerializer jss = new JavaScriptSerializer();
                     foreach (DataRow dataRow in RVTable.Rows)
                     {
                         notices.Add(ParseRV_v2(dataRow));
                     }
                 }
             }
             catch (Exception ex)
             {
                 Logger.Error(new
                 {
                     Action = "GetRVByOwnerId",
                     UserId = ownerId,
                     Message = ex.Message,
                     StackTrace = ex.StackTrace
                 }.ToJson());
             }
             return notices;
         }

        public RV GetRVById(string id)
        {
            RV rv = null;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id)
                                        };

                DataSet ds = DbHelper.ExecuteDataset(this._connectionString, CommandType.StoredProcedure, "[rv_get_by_id]", parms);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dataRow = ds.Tables[0].Rows[0];

                    rv = ParseRV(dataRow);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "GetRVById",
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());
            }
            return rv;
        }

        public RV GetRVByIdAndOwnerId(string id, string ownerId)
        {
            RV rv = null;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_OWNER_ID, DBParamNameConstant.RV_OWNER_ID_DB_TYPE, DBParamNameConstant.RV_OWNER_ID_SIZE, ownerId)
                                        };

                DataSet ds = DbHelper.ExecuteDataset(this._connectionString, CommandType.StoredProcedure, "[rv_get_by_id_ownerid]", parms);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dataRow = ds.Tables[0].Rows[0];

                    rv = ParseRV(dataRow);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "GetRVByIdAndOwnerId",
                    UserId = ownerId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());
            }
            return rv;
        }

        public bool UpdateAutomotive(string id, RVAutomotive automotive)
        {
            bool result = true;
            try
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string automotiveStr = jss.Serialize(automotive);
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_AUTOMOTIVE_JSON, DBParamNameConstant.RV_AUTOMOTIVE_JSON_DB_TYPE, DBParamNameConstant.RV_AUTOMOTIVE_JSON_SIZE, automotiveStr)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_automotive]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdateAutomotive",
                    RVId = id,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdateInterior(string id, RVInterior interior, byte sleeps)
        {
            bool result = true;
            try
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string interiorStr = jss.Serialize(interior);
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_INTERIORS_JSON, DBParamNameConstant.RV_INTERIORS_JSON_DB_TYPE, DBParamNameConstant.RV_INTERIORS_JSON_SIZE, interiorStr),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_SLEEPS, DBParamNameConstant.RV_SLEEPS_DB_TYPE, DBParamNameConstant.RV_SLEEPS_SIZE, sleeps)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_interior]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdateInterior",
                    RVId = id,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdatePricing(string id, RVPricing pricing)
        {
            bool result = true;
            try
            {
                string pricingJson = Serializer.ToJson(pricing);
                SqlParameter[] parms = {
                                           DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                           DbHelper.MakeInParam(DBParamNameConstant.RV_PRICING_JSON, DBParamNameConstant.RV_PRICING_JSON_DB_TYPE, DBParamNameConstant.RV_PRICING_JSON_SIZE, pricingJson)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_pricing]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdatePricing",
                    RVId = id,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdateOverview(string id, string title, string summary, byte type,
            string modelStyle, DateTime madeYear, byte madeBy, string made, bool IsAllowedHandleBooking)
        {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_TITLE, DBParamNameConstant.RV_TITLE_DB_TYPE, DBParamNameConstant.RV_TITLE_SIZE, title),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_SUMMARY, DBParamNameConstant.RV_SUMMARY_DB_TYPE, DBParamNameConstant.RV_SUMMARY_SIZE, summary),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_TYPE, DBParamNameConstant.RV_TYPE_DB_TYPE, DBParamNameConstant.RV_TYPE_SIZE, (byte)type),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MODEL_STYLE, DBParamNameConstant.RV_MODEL_STYLE_DB_TYPE, DBParamNameConstant.RV_MODEL_STYLE_SIZE, modelStyle),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MADE_YEAR, DBParamNameConstant.RV_MADE_YEAR_DB_TYPE, DBParamNameConstant.RV_MADE_YEAR_SIZE, madeYear),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MADE_BY, DBParamNameConstant.RV_MADE_BY_DB_TYPE, DBParamNameConstant.RV_MADE_BY_SIZE, (byte)madeBy),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_MADE, DBParamNameConstant.RV_MADE_DB_TYPE, DBParamNameConstant.RV_MADE_SIZE, made),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING, DBParamNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING_DB_TYPE, DBParamNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING_SIZE, IsAllowedHandleBooking)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_overview]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdateOverview",
                    RVId = id,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdateCalendar(string id, RVAvailabilityTypeEnum availability, IEnumerable<RVAvailableDateInterval> dateIntervals)
        {
            bool result = true;
            try
            {
                if (dateIntervals != null)
                {
                    foreach (RVAvailableDateInterval dateInterval in dateIntervals)
                    {
                        dateInterval.StartDate = DateTime.SpecifyKind(dateInterval.StartDate, DateTimeKind.Local);
                        dateInterval.StartDate = dateInterval.StartDate.ToUniversalTime();
                        dateInterval.EndDate = DateTime.SpecifyKind(dateInterval.EndDate, DateTimeKind.Local);
                        dateInterval.EndDate = dateInterval.EndDate.ToUniversalTime();
                    }
                }

                JavaScriptSerializer jss = new JavaScriptSerializer();
                string dateIntervalsStr = jss.Serialize(dateIntervals);
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, id),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_AVAILABLE_TYPE, DBParamNameConstant.RV_AVAILABLE_TYPE_DB_TYPE, DBParamNameConstant.RV_AVAILABLE_TYPE_SIZE, (byte)availability),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON, DBParamNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON_DB_TYPE, DBParamNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON_SIZE, dateIntervalsStr)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_calendar]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdateCalendar",
                    RVId = id,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public IEnumerable<RV> GetAllRV_Paging(int page, int pageSize)
        {
            int start = (page - 1) * pageSize;
            int count = pageSize;
            return GetAllRV(start, count);
        }

        public IEnumerable<RV> GetAllRV(int start, int count)
        {
            List<RV> RVs = new List<RV>();
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.START, DBParamNameConstant.START_DB_TYPE, DBParamNameConstant.START_SIZE, start),
                                            DbHelper.MakeInParam(DBParamNameConstant.COUNT, DBParamNameConstant.COUNT_DB_TYPE, DBParamNameConstant.COUNT_SIZE, count)
                                        };

                DataSet ds = DbHelper.ExecuteDataset(this._connectionString, CommandType.StoredProcedure, "[rv_get_all]", parms);

                if (ds != null && ds.Tables.Count > 0)
                {
                    DataTable RVTable = ds.Tables[0];
                    foreach (DataRow dataRow in RVTable.Rows)
                    {
                        RVs.Add(ParseRV_v2(dataRow));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "GetAllRV",
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());
            }
            return RVs;
        }

        public bool DeletePhoto(string photoId, string RVId)
        {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_ID, DBParamNameConstant.PHOTO_ID_DB_TYPE, DBParamNameConstant.PHOTO_ID_SIZE, photoId),
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_RV_ID, DBParamNameConstant.PHOTO_RV_ID_DB_TYPE, DBParamNameConstant.PHOTO_RV_ID_SIZE, RVId)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[photo_delete]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "DeletePhoto",
                    PhotoId = photoId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public IEnumerable<Photo> GetPhotoByRVId(string RVId)
        {
            List<Photo> photos = new List<Photo>();
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_RV_ID, DBParamNameConstant.PHOTO_RV_ID_DB_TYPE, DBParamNameConstant.PHOTO_RV_ID_SIZE, RVId)
                                        };

                DataSet ds = DbHelper.ExecuteDataset(this._connectionString, CommandType.StoredProcedure, "[photo_get_by_RVid]", parms);

                if (ds != null && ds.Tables.Count > 0)
                {
                    DataTable photoTable = ds.Tables[0];
                    foreach (DataRow dataRow in photoTable.Rows)
                    {
                        photos.Add(new Photo
                        {
                            Id = (string)dataRow[DBColumnNameConstant.PHOTO_ID],
                            RVId = (string)dataRow[DBColumnNameConstant.PHOTO_RV_ID],
                            Description = (dataRow[DBColumnNameConstant.PHOTO_DESCRIPTION] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.PHOTO_DESCRIPTION],
                            CreatTime = (DateTime)dataRow[DBColumnNameConstant.PHOTO_CREATE_TIME]
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "GetPhotoByRVId",
                    RVId = RVId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());
            }
            return photos;
        }

        public bool InsertPhoto(Photo photo)
        {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_ID, DBParamNameConstant.PHOTO_ID_DB_TYPE, DBParamNameConstant.PHOTO_ID_SIZE, photo.Id),
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_RV_ID, DBParamNameConstant.PHOTO_RV_ID_DB_TYPE, DBParamNameConstant.PHOTO_RV_ID_SIZE, photo.RVId),
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_CREATE_TIME, DBParamNameConstant.PHOTO_CREATE_TIME_DB_TYPE, DBParamNameConstant.PHOTO_CREATE_TIME_SIZE, photo.CreatTime)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[photo_insert]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "InsertPhoto",
                    PhotoId = photo.Id,
                    RVId = photo.RVId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdatePhoto(string ownerId, string photoId, string RVId, string description)
        {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_OWNER_ID, DBParamNameConstant.RV_OWNER_ID_DB_TYPE, DBParamNameConstant.RV_OWNER_ID_SIZE, ownerId),
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_ID, DBParamNameConstant.PHOTO_ID_DB_TYPE, DBParamNameConstant.PHOTO_ID_SIZE, photoId),
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_RV_ID, DBParamNameConstant.PHOTO_RV_ID_DB_TYPE, DBParamNameConstant.PHOTO_RV_ID_SIZE, RVId),
                                            DbHelper.MakeInParam(DBParamNameConstant.PHOTO_DESCRIPTION, DBParamNameConstant.PHOTO_DESCRIPTION_DB_TYPE, DBParamNameConstant.PHOTO_DESCRIPTION_SIZE, description)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[photo_update]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdatePhoto",
                    OwnerId = ownerId,
                    RVId = RVId,
                    PhotoId = photoId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdateAddress(string RVId, string zip, string address)
        {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, RVId),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ZIP, DBParamNameConstant.RV_ZIP_DB_TYPE, DBParamNameConstant.RV_ZIP_SIZE, zip),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ADDRESS, DBParamNameConstant.RV_ADDRESS_DB_TYPE, DBParamNameConstant.RV_ADDRESS_SIZE, address)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_address]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdateAddress",
                    RVId = RVId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        public bool UpdateCoverPhoto(string RVId, string coverPhotoId)
        {
            bool result = true;
            try
            {
                SqlParameter[] parms = {
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_ID, DBParamNameConstant.RV_ID_DB_TYPE, DBParamNameConstant.RV_ID_SIZE, RVId),
                                            DbHelper.MakeInParam(DBParamNameConstant.RV_COVER_PHOTO_ID, DBParamNameConstant.RV_COVER_PHOTO_ID_DB_TYPE, DBParamNameConstant.RV_COVER_PHOTO_ID_SIZE, coverPhotoId)
                                       };
                DbHelper.ExecuteNonQuery(this._connectionString, CommandType.StoredProcedure, "[rv_update_cover_photo]", parms);
            }
            catch (Exception ex)
            {
                Logger.Error(new
                {
                    Action = "UpdateCoverPhoto",
                    RVId = RVId,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace
                }.ToJson());

                result = false;
            }

            return result;
        }

        private RV ParseRV(DataRow dataRow)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();

            RVAvailableDateInterval[] availableDateIntervals = null;
            if (!(dataRow[DBColumnNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON] is DBNull))
            {
                availableDateIntervals = jss.Deserialize<RVAvailableDateInterval[]>((string)dataRow[DBColumnNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON]);
            }

            if (availableDateIntervals != null)
            {
                foreach (RVAvailableDateInterval dateInterval in availableDateIntervals)
                {
                    dateInterval.StartDate = DateTime.SpecifyKind(dateInterval.StartDate, DateTimeKind.Utc);
                    dateInterval.StartDate = dateInterval.StartDate.ToLocalTime();
                    dateInterval.EndDate = DateTime.SpecifyKind(dateInterval.EndDate, DateTimeKind.Utc);
                    dateInterval.EndDate = dateInterval.EndDate.ToLocalTime();
                }
            }

            RVAutomotive automotive = null;
            if (!(dataRow[DBColumnNameConstant.RV_AUTOMOTIVE_JSON] is DBNull))
            {
                automotive = jss.Deserialize<RVAutomotive>((string)dataRow[DBColumnNameConstant.RV_AUTOMOTIVE_JSON]);
            }

            RVInterior Interior = null;
            if (!(dataRow[DBColumnNameConstant.RV_INTERIORS_JSON] is DBNull))
            {
                Interior = jss.Deserialize<RVInterior>((string)dataRow[DBColumnNameConstant.RV_INTERIORS_JSON]);
            }

            RVPricing pricing = null;
            if (!(dataRow[DBColumnNameConstant.RV_PRICING_JSON] is DBNull))
            {
                pricing = Serializer.FromJsonTo<RVPricing>((string)dataRow[DBColumnNameConstant.RV_PRICING_JSON]);
            }

            return new RV
            {
                Id = (string)dataRow[DBColumnNameConstant.RV_ID],
                OwnerId = (string)dataRow[DBColumnNameConstant.RV_OWNER_ID],
                Title = (dataRow[DBColumnNameConstant.RV_TITLE] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_TITLE],
                Summary = (dataRow[DBColumnNameConstant.RV_SUMMARY] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_SUMMARY],
                RVType = (RVTypeEnum)((byte)dataRow[DBColumnNameConstant.RV_TYPE]),
                ModelStyle = (dataRow[DBColumnNameConstant.RV_MODEL_STYLE] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_MODEL_STYLE],
                MadeYear = (DateTime)dataRow[DBColumnNameConstant.RV_MADE_YEAR],
                MadeBy = (RVMadeByEnum)((byte)dataRow[DBColumnNameConstant.RV_MADE_BY]),
                Made = (dataRow[DBColumnNameConstant.RV_MADE] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_MADE],
                Sleeps = (byte)dataRow[DBColumnNameConstant.RV_SLEEPS],
                Length = (double)dataRow[DBColumnNameConstant.RV_LENGTH],
                Zip = (dataRow[DBColumnNameConstant.RV_ZIP] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_ZIP],
                StateId = (int)dataRow[DBColumnNameConstant.RV_STATE_ID],
                CityId = (int)dataRow[DBColumnNameConstant.RV_CITY_ID],
                Address = (dataRow[DBColumnNameConstant.RV_ADDRESS] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_ADDRESS],
                AvailableType = (RVAvailabilityTypeEnum)((byte)dataRow[DBColumnNameConstant.RV_AVAILABLE_TYPE]),
                AvailableDateIntervals = availableDateIntervals,
                CoverPhotoId = (dataRow[DBColumnNameConstant.RV_COVER_PHOTO_ID] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_COVER_PHOTO_ID],
                IsAllowedHandleBooking = (bool)dataRow[DBColumnNameConstant.RV_IS_ALLOWED_HANDLE_BOOKING],
                Status = (RVStatus)((byte)dataRow[DBColumnNameConstant.RV_STATUS]),
                CreateTime = (DateTime)dataRow[DBColumnNameConstant.RV_CREATE_TIME],
                Pricing = pricing,
                Automotive = automotive,
                Interior = Interior
            };
        }

        private RV ParseRV_v2(DataRow dataRow)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();

            RVAvailableDateInterval[] availableDateIntervals = null;
            if (!(dataRow[DBColumnNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON] is DBNull))
            {
                availableDateIntervals = jss.Deserialize<RVAvailableDateInterval[]>((string)dataRow[DBColumnNameConstant.RV_AVAILABLE_DATE_INTERVALS_JSON]);
            }

            if (availableDateIntervals != null)
            {
                foreach (RVAvailableDateInterval dateInterval in availableDateIntervals)
                {
                    dateInterval.StartDate = DateTime.SpecifyKind(dateInterval.StartDate, DateTimeKind.Utc);
                    dateInterval.StartDate = dateInterval.StartDate.ToLocalTime();
                    dateInterval.EndDate = DateTime.SpecifyKind(dateInterval.EndDate, DateTimeKind.Utc);
                    dateInterval.EndDate = dateInterval.EndDate.ToLocalTime();
                }
            }

            RVPricing pricing = null;
            if (!(dataRow[DBColumnNameConstant.RV_PRICING_JSON] is DBNull))
            {
                pricing = Serializer.FromJsonTo<RVPricing>((string)dataRow[DBColumnNameConstant.RV_PRICING_JSON]);
            }

            return new RV
            {
                Id = (string)dataRow[DBColumnNameConstant.RV_ID],
                Title = (dataRow[DBColumnNameConstant.RV_TITLE] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_TITLE],
                RVType = (RVTypeEnum)((byte)dataRow[DBColumnNameConstant.RV_TYPE]),
                ModelStyle = (dataRow[DBColumnNameConstant.RV_MODEL_STYLE] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_MODEL_STYLE],
                MadeYear = (DateTime)dataRow[DBColumnNameConstant.RV_MADE_YEAR],
                MadeBy = (RVMadeByEnum)((byte)dataRow[DBColumnNameConstant.RV_MADE_BY]),
                Made = (dataRow[DBColumnNameConstant.RV_MADE] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_MADE],
                Sleeps = (byte)dataRow[DBColumnNameConstant.RV_SLEEPS],
                Length = (double)dataRow[DBColumnNameConstant.RV_LENGTH],
                Zip = (dataRow[DBColumnNameConstant.RV_ZIP] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_ZIP],
                StateId = (int)dataRow[DBColumnNameConstant.RV_STATE_ID],
                CityId = (int)dataRow[DBColumnNameConstant.RV_CITY_ID],
                Address = (dataRow[DBColumnNameConstant.RV_ADDRESS] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_ADDRESS],
                AvailableType = (RVAvailabilityTypeEnum)((byte)dataRow[DBColumnNameConstant.RV_AVAILABLE_TYPE]),
                AvailableDateIntervals = availableDateIntervals,
                Pricing = pricing,
                Status = (RVStatus)((byte)dataRow[DBColumnNameConstant.RV_STATUS]),
                CoverPhotoId = (dataRow[DBColumnNameConstant.RV_COVER_PHOTO_ID] is DBNull) ? null : (string)dataRow[DBColumnNameConstant.RV_COVER_PHOTO_ID],
                CreateTime = (DateTime)dataRow[DBColumnNameConstant.RV_CREATE_TIME]
            };
        }
    }
}
